package cn.com.darly.videoandroidpm.ui.registerfriend;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.darly.common.DLog;
import com.darly.common.ToastApp;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.clearedit.ClearEditText;
import com.darly.dview.widget.header.TitleView;

import java.util.List;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.bean.FriendInfo;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.ItemClickListenter;
import cn.com.darly.videoandroidpm.common.StaticListener;
import cn.com.darly.videoandroidpm.ui.adapter.AddFriendAdapter;
import cn.com.darly.videoandroidpm.ui.adapter.ApplyFriendAdapter;
import cn.com.darly.videoandroidpm.ui.biz.AddFriendListener.AddFriendInterface;
import cn.com.darly.videoandroidpm.ui.presenter.AddFriendPresenter;

/**
 * 添加好友页面，顶部一个搜索框，输入用户手机号。或者用户昵称，进行搜索用户。
 * Copyright (c) 2017 Organization D.L. zhangyuhui All rights reserved.
 *
 * @author Darly/张宇辉/2018/4/19 10:49
 * @version 1.0/cn.com.darly.videoandroidpm
 */
@ContentBinder(R.layout.activity_add_friend)
public class AddFriendActivity extends BaseActivity implements View.OnClickListener, AddFriendInterface {
    @ViewsBinder(R.id.id_base_title)
    public TitleView id_base_title;
    @ViewsBinder(R.id.id_add_friend_anim)
    LinearLayout id_add_friend_anim;
    @ViewsBinder(R.id.id_add_friend_search)
    ImageView id_add_friend_search;
    @ViewsBinder(R.id.id_add_friend_username)
    ClearEditText id_add_friend_username;
    @ViewsBinder(R.id.id_add_friend_userlist)
    ListView id_add_friend_userlist;
    @ViewsBinder(R.id.id_add_friend_userlist_no)
    TextView id_add_friend_userlist_no;

    @ViewsBinder(R.id.id_add_friend_sera)
    LinearLayout id_add_friend_sera;
    @ViewsBinder(R.id.id_add_friend_checker)
    ListView id_add_friend_checker;
    @ViewsBinder(R.id.id_add_friend_checker_no)
    TextView id_add_friend_checker_no;

    AddFriendAdapter adapter;
    ApplyFriendAdapter adapter_checker;
    AddFriendPresenter presenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        id_add_friend_sera.setAnimation(AnimationUtils.loadAnimation(this, R.anim.login_up));
        id_base_title.setTitle("添加好友");
        presenter = new AddFriendPresenter(this);
    }


    @Override
    protected void loadData() {
        adapter = new AddFriendAdapter(null, R.layout.item_add_friend, this);
        id_add_friend_userlist.setAdapter(adapter);
        adapter_checker = new ApplyFriendAdapter(null, R.layout.item_add_friend, this);
        id_add_friend_checker.setAdapter(adapter_checker);
        presenter.applyChecker();
    }

    @Override
    protected void initListener() {
        id_add_friend_search.setOnClickListener(this);
        id_base_title.setLeftBackOneListener(R.mipmap.ic_title_back, this);
        id_base_title.setRightViewRightTextOneListener("查找", this);
        //跳转过去判断是否申请添加好友
        id_add_friend_userlist.setOnItemClickListener(new ItemClickListenter() {
            @Override
            public void NoDoubleItemClickListener(AdapterView<?> parent, View view, int position, long id) {
                LoginInfo info = (LoginInfo) parent.getItemAtPosition(position);
                if (info != null) {
                    int up = info.getFriendup();
                    if (up == -1) {
                        Intent intent = new Intent(AddFriendActivity.this, ApplyFriendActivity.class);
                        intent.putExtra("LoginInfo", info);
                        AddFriendActivity.this.startActivityForResult(intent, 10102);
                    } else if (up == 0) {
                        ToastApp.showToast("已经申请添加" + info.getName() + "为好友，请耐心等待对方同意");
                    } else {
                        //好友等级区分1-100,普通好友，100-1000，真心好友，1000+,至尊好友
                        String test = null;
                        if (up > 0 && up <= 100) {
                            test = "普通好友";
                        } else if (up > 100 && up <= 1000) {
                            test = "真心好友";
                        } else {
                            test = "至尊好友";
                        }

                        ToastApp.showToast(test);
                    }
                }
            }
        });
        //跳转过去是否同意用户添加好友操作
        id_add_friend_checker.setOnItemClickListener(new ItemClickListenter() {
            @Override
            public void NoDoubleItemClickListener(AdapterView<?> parent, View view, int position, long id) {
                FriendInfo info = (FriendInfo) parent.getItemAtPosition(position);
                if (info != null) {
                    if (info.isApply()) {
                        ToastApp.showToast("申请中，请等候好友验证通过");
                    } else {
                        Intent intent = new Intent(AddFriendActivity.this, BeenFriendActivity.class);
                        intent.putExtra("FriendInfo", info);
                        AddFriendActivity.this.startActivityForResult(intent, 10101);
                    }
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_view_ok:
                if (id_base_title.getTitle().equals("添加好友")) {
                    id_base_title.setTitle("搜索好友");
                    id_base_title.setRightViewRightTextOneListener("添加", this);
                    id_add_friend_sera.setVisibility(View.GONE);
                    id_add_friend_anim.setVisibility(View.VISIBLE);
                    id_add_friend_anim.setAnimation(AnimationUtils.loadAnimation(this, R.anim.login_up));
                } else {
                    id_base_title.setTitle("添加好友");
                    id_base_title.setRightViewRightTextOneListener("查找", this);
                    id_add_friend_anim.setVisibility(View.GONE);
                    id_add_friend_sera.setVisibility(View.VISIBLE);
                    id_add_friend_sera.setAnimation(AnimationUtils.loadAnimation(this, R.anim.login_up));
                }
                break;
            default:
                presenter.onClickDown(this, v);
                break;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            end();
            return true;//屏蔽按钮
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onSearchSuccess(List<LoginInfo> loginInfos) {
        if (loginInfos != null && loginInfos.size() > 0) {
            adapter.setData(loginInfos);
            id_add_friend_userlist_no.setVisibility(View.GONE);
            id_add_friend_userlist.setVisibility(View.VISIBLE);
        } else {
            id_add_friend_userlist_no.setVisibility(View.VISIBLE);
            id_add_friend_userlist.setVisibility(View.GONE);
        }

    }

    @Override
    public void onSearchFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public void onAddFriendSuccess(Object code) {
        //申请好友成功，消息发送到服务端
        end();
    }

    @Override
    public void onAddFriendFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public void onApplySuccess(List<FriendInfo> friendInfos) {
        if (friendInfos != null && friendInfos.size() > 0) {
            adapter_checker.setData(friendInfos);
            id_add_friend_checker_no.setVisibility(View.GONE);
            id_add_friend_checker.setVisibility(View.VISIBLE);
        } else {
            id_add_friend_checker_no.setVisibility(View.VISIBLE);
            id_add_friend_checker.setVisibility(View.GONE);
        }
    }

    @Override
    public void onApplyFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public String findFriendName() {
        return id_add_friend_username.getText().toString().trim();
    }

    @Override
    public void end() {
        StaticListener.refreshFriend();
        finish();
    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 10101 && resultCode == 200) {
            DLog.i("onActivityResult");
            presenter.applyChecker();
        } else if (requestCode == 10102 && resultCode == 200) {
            DLog.i("onActivityResult");
            if (TextUtils.isEmpty(findFriendName())) {
                ToastApp.showToast("用户不能为空");
            } else {
                presenter.searchFriend();
            }
        }
    }
}
